#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
@author: Lénia Andrade nº 5607

http://matplotlib.sourceforge.net/examples/api/barchart_demo.html
'''

import matplotlib
matplotlib.use('QT4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
 
from Statistics import Statistics
import matplotlib.pyplot as plt
import numpy as np
import os.path


class CreateGraphics:
    
    
    '''
    This method creates the graphic referent to the first statistic (establishments per year)
    '''
     
    def graph_establishments_year(self):
           
        if os.path.exists('../graphics/Establishments_Year.png'):
            os.remove('../graphics/Establishments_Year.png')
            pass
                
        
        stat = Statistics()
    
        estabs_year = {}
        for v in range(2000, 2011):
            estabs_year.update({v:len(stat.get_establishments_year(v))})    
            pass
        
        N = len(estabs_year.keys())

        ind = np.arange(N)
        widthB = 0.8
        
        rects = plt.bar(ind, estabs_year.values(), width=widthB, bottom=0, color=('b', 'g', 'r', 'c', 'm', 'y', 'pink', 'w', '#ee7600', 'brown', '#919191'), xerr=0, yerr=0)
            
        plt.xticks(ind + (widthB / 2), estabs_year.keys())
            
        plt.xlabel("Years")
        plt.ylabel("Number establishments")
            
        plt.grid(True)

        self.autolabel(rects, estabs_year.values())
        
        
        plt.savefig("../graphics/Establishments_Year.png", dpi=70)
        
        plt.close()
        pass
    
    

    '''
    This method creates the graphic referent to the second statistic
    (teachers divided per categories per establishment and per year)
    '''
    def graph_teachers_cat_estab_year(self, year, estab):
        
       
        if os.path.exists('../graphics/Teachers_Category_Establishment_Year.png'):
            os.remove('../graphics/Teachers_Category_Establishment_Year.png')
            pass
        
        
        stat = Statistics()
        
        teachers_cat = {}
        categories = stat.cat_estab_year(year, estab)
        for c in categories:
            teachers_cat.update({c:len(stat.get_teachers_cat_estab_year(year, estab, c))}) 
            pass 
        
        
        N = len(teachers_cat.keys())

        ind = np.arange(N)
        
        widthB = 0.8
        
        rects = plt.bar(ind, teachers_cat.values(), width=widthB, bottom=0,
                             color=('b', 'g', 'r', 'c', 'm', 'y', 'pink', 'w', '#ee7600', 'brown', '#919191'), xerr=0, yerr=0)
            
        plt.xticks(ind + (widthB / 2), teachers_cat.keys())
        
        plt.xlabel("Categories")
        plt.ylabel("Number of teachers")    
        
        plt.grid(True)

        self.autolabel(rects, teachers_cat.values())
        
        title = "Number of teachers per category \n belonging to {0} in year {1}".format(estab.encode('utf-8'), year.encode('utf-8'))  
        plt.title(title)
        plt.savefig("../graphics/Teachers_Category_Establishment_Year.png", dpi=70)
        plt.close()
        
        pass 
    
    
    '''
    This method creates graphic referent to third statistic 
    (teachers divided per type of degree per establishment per category and per year)
    '''
    
    def graph_teachers_deg_estab_cat_year(self, year, estab, cat):
        
        if os.path.exists('../graphics/Teachers_Degree_Establishment_Category_Year.png'):
            os.remove('../graphics/Teachers_Degree_Establishment_Category_Year.png')
            pass
        
        
        stat = Statistics()
        
        teachers_deg = {}
        degrees = stat.deg_cat_estab_year(year, estab, cat)
        
        for d in degrees:
            teachers_deg.update({d:len(stat.get_teachers_deg_estab_cat_year(year, estab, d, cat))}) 
            pass 
        
        
        N = len(teachers_deg.keys())

        ind = np.arange(N)
        
        widthB = 0.8
        
        rects = plt.bar(ind, teachers_deg.values(), width=widthB, bottom=0,
                             color=('b', 'g', 'r', 'c', 'm', 'y', 'pink', 'w', '#ee7600', 'brown', '#919191'), xerr=0, yerr=0)
            
        plt.xticks(ind + (widthB / 2), teachers_deg.keys())
        
        plt.xlabel("Degrees")
        plt.ylabel("Number of teachers")    
        
        plt.grid(True)

        self.autolabel(rects, teachers_deg.values())
        
        title = "Number of teachers per degree with {2} \n belonging to {0} in year {1}".format(estab.encode('utf-8'), year.encode('utf-8'), cat.encode('utf-8'))  
        plt.title(title)
        plt.savefig("../graphics/Teachers_Degree_Establishment_Category_Year.png", dpi=70)
        plt.close()
                
        pass
     
     
     
    '''
     this method add's the result in the top of a bar chart
    '''
    def autolabel(self, rects, valuesArray):
        
        i = 0
        for rect in rects:
            height = rect.get_height()
            plt.text(rect.get_x() + rect.get_width() / 2.,
                      1.005 * height,
                      '%s' % (valuesArray[i]),
                      ha='center', va='bottom')
            i = i + 1
            pass
        pass         
    pass

